package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.AccessRepository
import com.kzd.gtgame.main.AccessAction
import com.kzd.gtgame.utils.httpSign1
import com.kzd.gtgame.utils.httpSign2
import com.kzd.gtgame.utils.httpTime
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataT
import com.module.lemlin.http.SubscribeViewModel
import org.greenrobot.eventbus.EventBus

class AccessViewModel(application: Application) : BaseViewModel(application) {


    private val mAccessRepository = AccessRepository()

    lateinit var mAccessAction: AccessAction

    val accessGetCodeLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun accessGetCode(phone: String, type: Int = 1) {
        setRxLifeScope(SubscribeViewModel(accessGetCodeLiveData)) {
            mAccessRepository.accessGetCode(phone, type)
        }
    }


    val accessUserLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AccessUser>>>()
    private val accessUserSubscribe = object :
        SubscribeViewModel<HttpResponseDataT<AccessUser>>(accessUserLiveData) {
        override fun onSuccess(resp: HttpResponseDataT<AccessUser>) {
            super.onSuccess(resp)
            SPSConstance.uid = resp.data.uid
            SPSConstance.userInfo = resp.data.userInfo
            SPSConstance.userMine = resp.data.userMine

            EventBus.getDefault().post(IsLogin(isLogin = true))
        }
    }

//    val accessRegNameLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AccessUser>>>()
    fun accessRegName(
        userName: String, passwd: String, type: Int = 0, code: String? = null,
    ) {
        setRxLifeScope(accessUserSubscribe) {
            val sessionid = if (type == 1) accessGetCodeLiveData.value?.data?.data else null
            mAccessRepository.accessRegName1(
                userName = userName,
                passwd = passwd,
                type = type.toString(),
                code = code,
                sessionid = sessionid
            )
        }
    }

//    val accessLoginNameLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AccessUser>>>()
    fun accessLoginName(userName: String, passwd: String) {
        setRxLifeScope(accessUserSubscribe) {
            mAccessRepository.accessLoginName1(
                userName = userName, passwd = passwd,
                time = httpTime, sign = httpSign2(userName, passwd),
            )
        }
    }

    fun accessLoginOneKey(token: String) {
        setRxLifeScope(accessUserSubscribe) {
            mAccessRepository.accessLoginOneKey(
                token = token, time = httpTime, sign = httpSign2(token),
            )
        }
    }

    val accessChangePwdLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun accessChangePwd(
        oldpasswd: String, passwd: String, repasswd: String
    ) {
        setRxLifeScope(SubscribeViewModel(accessChangePwdLiveData)) {
            mAccessRepository.accessChangePwd(
                time = httpTime, sign = httpSign1(SPSConstance.uid), oldpasswd = oldpasswd,
                passwd = passwd, repasswd = repasswd
            )
        }
    }

    val accessSettingPwdLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun accessSettingPwd(
        passwd: String, repasswd: String
    ) {
        setRxLifeScope(SubscribeViewModel(accessSettingPwdLiveData)) {
            mAccessRepository.accessSettingPwd(
                passwd = passwd, repasswd = repasswd
            )
        }
    }

    val accessBindPhoneLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserInfo>>>()
    fun accessBindPhone(
        phone: String, code: String
    ) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataT<UserInfo>>(accessBindPhoneLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserInfo>) {
                super.onSuccess(resp)
                SPSConstance.userInfo = resp.data
            }
        }) {
            val sessionid = accessGetCodeLiveData.value?.data?.data ?: ""
            mAccessRepository.accessBindPhone(
                time = httpTime,
                sign = httpSign1(SPSConstance.uid),
                phone = phone,
                sessionid = sessionid,
                code = code
            )
        }
    }

    val accessVerifyPhoneLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun accessVerifyPhone(
        phone: String, code: String
    ) {
        setRxLifeScope(SubscribeViewModel(accessVerifyPhoneLiveData)) {
            val sessionid = accessGetCodeLiveData.value?.data?.data ?: ""
            mAccessRepository.accessVerifyPhone(
                phone = phone, code = code, sessionid = sessionid
            )
        }
    }

    val accessForgetPwdLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun accessForgetPwd(
        phone: String, passwd: String, code: String
    ) {
        setRxLifeScope(SubscribeViewModel(accessForgetPwdLiveData)) {
            val sessionid = accessGetCodeLiveData.value?.data?.data ?: ""
            mAccessRepository.accessForgetPwd(
                phone = phone,
                passwd = passwd,
                confirm_passwd = passwd,
                code = code,
                sessionid = sessionid
            )
        }
    }

}